/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.vdialog.model.instance;

import javax.jcr.RepositoryException;
import javax.jcr.ValueFormatException;
import offset.nodes.Constants;
import offset.nodes.client.model.StringArrayConverter;
import offset.nodes.client.model.TreeNodeUserObject;
import offset.nodes.client.virtual.model.jcr.SimpleNode;
import offset.nodes.client.virtual.model.jcr.SimpleProperty;
import offset.nodes.client.virtual.model.jcr.SimpleValue;

/**
 *
 * @author Walter Lütgenau
 */
public abstract class AbstractTemplateNodeInstance extends SimpleNode implements TreeNodeUserObject {

    String type;

    public AbstractTemplateNodeInstance(String type) {
        super(null, null);
        this.type = type;
    }

    protected String getStringProperty(String name) {
        if (!hasProperty(name)) {
            return null;
        }

        return ((SimpleProperty) getProperty(name)).getString();
    }

    protected Boolean getBooleanProperty(String name) throws RepositoryException {
        if (!hasProperty(name)) {
            return null;
        }

        return ((SimpleProperty) getProperty(name)).getBoolean();
    }

    protected void setBooleanProperty(String name, boolean value) throws RepositoryException {
        setProperty(name, value);
    }

    public String[] getStringArrayProperty(String name) throws RepositoryException {
            if (!hasProperty(name)) {
                return null;
            }
            StringArrayConverter sa = new StringArrayConverter(getProperty(name).getString());
            return sa.toArray();
    }
    
    public void setStringArrayProperty(String name, String[] value) throws RepositoryException {
        if (getProperty(name) == null) {
            setProperty(name, new SimpleProperty(name, value));
            return;
        }
        StringArrayConverter sa = new StringArrayConverter(value);
        getProperty(name).setValue(sa.toString());
    }

    public String getQuery() {
        return getStringProperty(Constants.PROP_PRIMARY_QUERY);
    }

    public void setQuery(String query) {
        setProperty(Constants.PROP_PRIMARY_QUERY, query);
    }
    
    public String getNodeType() {
        return getStringProperty(Constants.PROP_NODETYPE);
    }

    public String getTemplateNodeReference() {
        return getStringProperty(Constants.PROP_TEMPLATE_NODE_REFERENCE);
    }

    public void setTemplateNodeReference(String templateNodeReference) {
        setProperty(Constants.PROP_TEMPLATE_NODE_REFERENCE, templateNodeReference);
    }

    public String getType() {
        return type;
    }
    
    public String toString() {
        // return the name, if present
        String name = getStringProperty(Constants.PROP_NAME);
        if ( name != null)
            return name;
        
        // else return the name property
        name = getStringProperty(Constants.PROP_NAME_PROPERTY);
        return "@"+name;
    }
}
